package com.zzyl.serve.service.impl;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.redis.RedisCache;
import com.zzyl.common.utils.DateUtils;

import com.zzyl.serve.dto.NursingLevelDTO;
import com.zzyl.serve.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingLevelMapper;
import com.zzyl.serve.domain.NursingLevel;
import com.zzyl.serve.service.INursingLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 护理等级Service业务层处理
 *
 * @author liyi
 * @date 2025-10-13
 */
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService {

    private static final Integer ENABLE = 1;

    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    private static final String NURSINGLEVEL_ALL = "nursinglevel:all";

    /**
     * 查询护理等级
     *
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Integer id) {
        return this.getById(id);
    }

    /**
     * 查询护理等级列表
     *
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevelVo> selectNursingLevelList(NursingLevel nursingLevel) {
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     *
     * @param nursingLevelDto 护理等级Dto
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevelDTO nursingLevelDto) {
        NursingLevel nursingLevel = BeanUtil.copyProperties(nursingLevelDto, NursingLevel.class);
        // 清理缓存
        int row = nursingLevelMapper.insertNursingLevelDto(nursingLevel);
        clearNursingLevelCache();
        return row;

    }

    /**
     * 修改护理等级
     *
     * @param nursingLevelDTO 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevelDTO nursingLevelDTO) {
        NursingLevel nursingLevel = BeanUtil.copyProperties(nursingLevelDTO, NursingLevel.class);
        boolean flag = this.updateById(nursingLevel);
        // 清理缓存
        clearNursingLevelCache();
        return flag ? 1 : 0;
    }

    /**
     * 批量删除护理等级
     *
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Integer[] ids) {
        boolean flag = this.removeBatchByIds(Arrays.asList(ids));
        // 清理缓存
        clearNursingLevelCache();
        return flag ? 1 : 0;
    }

    /**
     * 删除护理等级信息
     *
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Integer id) {
        boolean flag = this.removeById(id);
        // 清理缓存
        clearNursingLevelCache();
        return flag ? 1 : 0;
    }

    /**
     * 查询所有护理等级
     *
     * @return
     */
    @Override
    public List<NursingLevel> getLevel() {
        // 缓存优化
        List<NursingLevel> nursingLevelList = (List<NursingLevel>) redisTemplate.opsForValue().get(NURSINGLEVEL_ALL);
        if (CollectionUtils.isNotEmpty(nursingLevelList)) {
            return nursingLevelList;
        }
        // 从数据库中查
        nursingLevelList = this.list(Wrappers.<NursingLevel>lambdaQuery().eq(NursingLevel::getStatus, ENABLE));
        redisTemplate.opsForValue().set(NURSINGLEVEL_ALL, nursingLevelList);
        return nursingLevelList;

    }


    /**
     * 清理缓存
     */
    private void clearNursingLevelCache() {
        redisTemplate.delete(NURSINGLEVEL_ALL);
    }
}
